﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

using MapLibrary.AILibrary.Pathfinding;
using Animation;
using MapLibrary;
using FarseerPhysics.Dynamics;
using FarseerPhysics;
using Enemies;
using MapLibrary.Utility;

namespace AI
{
    public class MalariaBot : Bot
    {
        private const int ATTACK_COOLDOWN = 60;
        private const int STUN_FRAMES = 120;

        private List<Node> currentPath;
        private int timeToUpdatePath;
        public int TimeToAttack { get; set; }

        private bool wasOnScreen;

        private TimerUtility stunTimer;

        private bool stunned;

        public bool IsInNet { get; set; }

        public MalariaBot(Sprite owner) : base(owner)
        {
            Init();
        }

        public MalariaBot()
        {
            Init();
        }

        private void Init()
        {
            currentPath = new List<Node>();
            IsInNet = false;
            wasOnScreen = false;
            timeToUpdatePath = 60;
            TimeToAttack = ATTACK_COOLDOWN;
            stunTimer = new TimerUtility(Unstun, STUN_FRAMES);
        }

        public void ResetAttackTime()
        {
            TimeToAttack = ATTACK_COOLDOWN;
        }

        public void Stun()
        {
            stunned = true;
            Owner.Body.IgnoreGravity = false;
        }

        public void Unstun()
        {
            stunned = false;
            Owner.Body.IgnoreGravity = true;
        }

        public override void Update(Level level, World world)
        {
            timeToUpdatePath--;
            TimeToAttack--;
            TimeToAttack = (TimeToAttack < 0) ? 0 : TimeToAttack;

            Malaria mal = Owner as Malaria;

            if (stunned)
            {
                stunTimer.Update();
                return;
            }

            if (mal.Net != null)
            {
                return;
            }

            if (timeToUpdatePath <= 0)
            {
                if (!wasOnScreen)
                {
                    if (!level.World.Viewport.IsRectangleInViewport(new Rectangle((int)Owner.Position.X, (int)Owner.Position.Y, Owner.CurrentTexture.Width, Owner.CurrentTexture.Height)))
                    {
                        Owner.Body.LinearVelocity = Vector2.Zero;
                        return;
                    }
                    else
                    {
                        wasOnScreen = true;
                    }
                }


                timeToUpdatePath = 6;


                if (level.LevelGraph.GetPath(Owner, level.Player, ref currentPath))
                {
                    Vector2 dir;
                    if (currentPath.Count > 3)
                    {
                        Node n = currentPath[1];
                        dir = n.Position - Owner.Position;
                        dir.Normalize();
                        Owner.Body.ApplyLinearImpulse(dir);
                    }
                    else
                    {
                        Vector2 offset = new Vector2(GameWorld.Random.Next(0, 25), GameWorld.Random.Next(-200, 200));
                        dir = (level.Player.Position + offset) - Owner.Position;
                        dir.Normalize();
                        Owner.Body.ApplyLinearImpulse(dir);
                    }

                    if (dir.X <= 0)
                    {
                        if(!Owner.Type.CurrentState.Equals("Left"))
                            Owner.Type.CurrentState = "Left";
                    }
                    else
                    {
                        if(!Owner.Type.CurrentState.Equals("Right"))
                            Owner.Type.CurrentState = "Right";
                    }
                }
                else
                {
                    Owner.Body.LinearVelocity = Vector2.Zero;
                }
            }
        }
    }
}
